home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1996 #15 / Monster Media Number 15 (Monster Media)(July 1996).ISO / math / alged34.zip / ALGEDSRC.ZIP / ALGNODE.C < prev    next >
C/C++ Source or Header  |  1996-06-06  |  4KB  |  174 lines

  1. /*--------------------------------------------------------------------
  2.    Alged:  Algebra Editor    henckel@vnet.ibm.com
  3.  
  4.    Copyright (c) 1994 John Henckel
  5.    Permission to use, copy, modify, distribute and sell this software
  6.    and its documentation for any purpose is hereby granted without fee,
  7.    provided that the above copyright notice appear in all copies.
  8. */
  9. #include "alged.h"
  10. /*-----------------------------------------------------------------
  11.    memory allocation
  12. */
  13. void checknull(void *p) {
  14.   if (!p) {
  15.     printf(msg[20]);  /* heap memory is all used up! */
  16.     delay(2000);
  17.     exit(1);
  18.   }
  19. }
  20.  
  21. node *newnode(void) {
  22.   node *p;
  23.   p = malloc(sizeof *p); checknull(p);
  24.   p->tag = 12345;
  25.   p->nump = 0;
  26.   p->msg = NULL;
  27.   p->kind = VAR;
  28.   strcpy(p->name,"??");
  29.   return p;
  30. }
  31.  
  32. node *newnum(double val) {
  33.   node *p;
  34.   p = malloc(sizeof *p); checknull(p);
  35.   p->tag = 12345;
  36.   p->nump = 0;
  37.   p->msg = NULL;
  38.   p->kind = NUM;
  39.   p->value = val;
  40.   return p;
  41. }
  42.  
  43. node *newvar(char *s) {
  44.   node *p;
  45.   p = newnode();
  46.   strcpy(p->name,s);
  47.   return p;
  48. }
  49.  
  50. node *newoper(int kind) {
  51.   node *p;
  52.   p = malloc(sizeof *p); checknull(p);
  53.   p->tag = 12345;
  54.   p->nump = 2;
  55.   p->msg = NULL;
  56.   p->kind = kind;
  57.   p->lf = p->rt = NULL;
  58.   strcpy(p->name,kname[kind]);
  59.   return p;
  60. }
  61.  
  62. void freenode(node *p) {
  63.   if (!p || p->tag != 12345) {
  64.     printf(msg[21]);
  65.     pause; return;
  66.   }
  67.   p->tag = 0;
  68.   p->nump = 0;
  69. /*  if (p->msg) free(p->msg);  */
  70.   p->msg = NULL;
  71.   p->kind = VAR;
  72.   strcpy(p->name,msg[23]);
  73.   free(p);
  74. }
  75.  
  76. void freetree(node *p) {
  77.   int i,n;
  78.   if (!p || p->tag != 12345) {
  79.     printf(msg[22]);
  80.     pause; return;
  81.   }
  82.   n = p->nump;
  83.   p->tag = 0;
  84.   p->nump = 0;
  85. /*  if (p->msg) free(p->msg);      */
  86.   p->msg = NULL;
  87.   p->kind = VAR;
  88.   strcpy(p->name,msg[23]); /* FREE */
  89.   for (i=0; i<n; ++i)
  90.     freetree(p->parm[i]);
  91.   free(p);
  92. }
  93.  
  94. /*-----------------------------------------------------------------
  95.    nodecpy, use this to copy one node over another.  The only
  96.    thing is that "next" is preserved to not disrupt the main
  97.    node linked list.
  98. */
  99. void nodecpy(node *a, node *b) {
  100.   node *nx;
  101.   nx = a->next;
  102.   memcpy(a,b,sizeof(*a));
  103.   a->next = nx;
  104. }
  105.  
  106. /*-----------------------------------------------------------------
  107.    set a=b,
  108.    frees a, then copies b into it and frees b.
  109. */
  110. void movenode(node *a, node *b) {
  111.   int i;
  112.   for (i=0; i<a->nump; ++i)
  113.     freetree(a->parm[i]);        /* free children */
  114.   nodecpy(a,b);
  115.   freenode(b);
  116. }
  117.  
  118. /*-----------------------------------------------------------------
  119.    cons  -- create a new node with left,opr,right or if left is
  120.         null, just return right.
  121. */
  122. node *cons(node *left,int opr,node *right) {
  123.   node *a;
  124.   if (!left) return right;       /* empty list */
  125.   a = newoper(opr);
  126.   a->lf = left;
  127.   a->rt = right;
  128.   return a;
  129. }
  130. /*-----------------------------------------------------------------
  131.    deepcopy a node
  132. */
  133. node *deepcopy(node *p) {
  134.   int j;
  135.   node *t;
  136.   t = newnode();
  137.   nodecpy(t,p);
  138.   for (j=0; j<p->nump; ++j)
  139.     t->parm[j] = deepcopy(p->parm[j]);
  140.   return t;
  141. }
  142. /*-----------------------------------------------------------------
  143.    lastnode
  144. */
  145. node *lastnode(node *p) {
  146.   while (p && p->next) p=p->next;
  147.   return p;
  148. }
  149.  
  150. /*-----------------------------------------------------------------
  151.    prevnode
  152. */
  153. node *prevnode(node *p) {
  154.   node *t;
  155.   for (t=firf; t && t->next!=p; ) t=t->next;
  156.   return t;
  157. }
  158.  
  159. /*-----------------------------------------------------------------
  160.    reverse the node list
  161. */
  162. node *reverse(node *b) {
  163.   node *a,*c;
  164.   a = NULL;
  165.   while (b) {
  166.     c = b->next;
  167.     b->next = a;
  168.     a = b;
  169.     b = c;
  170.   }
  171.   return a;
  172. }
  173.  
  174.